float3 LightPosition;

float4 DiffuseColor;
float3 EmissiveColor;	
float3 AmbientColor;	
float2 UVFactor;

float4 Booleans01;				// x = Diffuse

// Structs
struct VS_OUTPUT
{
    float4 position				: POSITION;
	float2 textureCoords		: TEXCOORD0;
	float3 positionW			: TEXCOORD1;
	float3 normalW				: TEXCOORD2;
	float3 tangentW				: TEXCOORD3;
	float3 binormalW			: TEXCOORD4;
};

// Samplers
sampler2D DiffuseSampler		: register(s0);
sampler2D BumpSampler			: register(s1);
sampler2D SpecularSampler		: register(s2);

sampler2D AlphaChannelsSampler : register(s3);

sampler2D RedChannelSampler : register(s4);
sampler2D GreenChannelSampler : register(s5);
sampler2D BlueChannelSampler : register(s6);
sampler2D RedChannelBumpSampler : register(s7);
sampler2D GreenChannelBumpSampler : register(s8);
sampler2D BlueChannelBumpSampler : register(s9);
sampler2D RedChannelSpecularSampler : register(s10);
sampler2D GreenChannelSpecularSampler : register(s11);
sampler2D BlueChannelSpecularSampler : register(s12);

float3 colorFor(VS_OUTPUT In, sampler2D diffuseSampler, sampler2D bumpSampler, sampler2D specularSampler, float3 lightDirectionW, float3x3 bumpMatrix, bool useUVFactor)
{
float3 baseColor = 1;
    float3 normalW =0;
	// Bump
	if (Booleans01.y == 1.0f)
	{
	    float3 normalSample = tex2D(bumpSampler, In.textureCoords).rgb;


		normalW = normalize(In.normalW + mul(normalSample, bumpMatrix));
	}
	if(useUVFactor)
	{
		baseColor = tex2D(diffuseSampler, In.textureCoords* UVFactor).rgb;
	}
	else
	{
		baseColor = tex2D(diffuseSampler, In.textureCoords).rgb;
	}
		//float2 decalsUv = In.textureCoords * UVFactor;
		//float3 alphaChannels = tex2D(AlphaChannelsSampler, In.textureCoords).rgb;
		//float3 rChannel = tex2D(RedChannelSampler, decalsUv).rgb;
		//float3 gChannel = tex2D(GreenChannelSampler, decalsUv).rgb;
		//float3 bChannel = tex2D(BlueChannelSampler, decalsUv).rgb;
		//baseColor = rChannel * alphaChannels.r + baseColor * (1-alphaChannels.r);
		//baseColor = gChannel * alphaChannels.g + baseColor * (1-alphaChannels.g);
		//baseColor = bChannel * alphaChannels.b + baseColor * (1-alphaChannels.b);
	

	// Diffuse Lighting
	float ndl = saturate(dot(normalW, lightDirectionW));

	// Specular Lighting	
	//float3 specularColor = SpecularColor.rgb;
	//float3 viewDirectionW = normalize(CameraPosition - In.positionW);
	//float3 reflectionVector = normalize(reflect(-lightDirectionW, normalW));
	//float specComp = pow(saturate(dot(reflectionVector, viewDirectionW)), SpecularColor.a);
////
	////if (Booleans01.z == 1.0f)
		////specularColor += tex2D(specularSampler, In.textureCoords).rgb;
//
	//float3 specular = specularColor * specComp;
	
	// Final
	return float3( EmissiveColor + (AmbientColor*baseColor) + ndl * baseColor );
}

// Shaders
float4 main(VS_OUTPUT In) : COLOR
{
		float3x3 bumpMatrix;
		bumpMatrix[0] = In.tangentW;
		bumpMatrix[1] = In.binormalW;
		bumpMatrix[2] = In.normalW;
	float3 lightDirectionW = normalize(LightPosition - In.positionW);
	float3 baseColor = colorFor(In, DiffuseSampler, BumpSampler, SpecularSampler, lightDirectionW, bumpMatrix,false);
	float2 decalsUv = In.textureCoords * UVFactor;
	float3 alphaChannels = tex2D(AlphaChannelsSampler, In.textureCoords).rgb;
	float3 rChannel = colorFor(In, RedChannelSampler, RedChannelBumpSampler, RedChannelSpecularSampler,lightDirectionW, bumpMatrix,true);
	float3 gChannel = colorFor(In, GreenChannelSampler, GreenChannelBumpSampler, GreenChannelSpecularSampler,lightDirectionW, bumpMatrix,true);
	float3 bChannel = colorFor(In, BlueChannelSampler, BlueChannelBumpSampler, BlueChannelSpecularSampler, lightDirectionW, bumpMatrix,true);
	baseColor = rChannel * alphaChannels.r + baseColor * (1-alphaChannels.r);
	baseColor = gChannel * alphaChannels.g + baseColor * (1-alphaChannels.g);
	baseColor = bChannel * alphaChannels.b + baseColor * (1-alphaChannels.b);
	return float4(baseColor, DiffuseColor.a);
}